package dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import dao.IUserinfoDAO;
import entity.Userinfo;

public class IUserinfoDAOImpl extends HibernateDaoSupport implements IUserinfoDAO {
	String hql = null;
	@SuppressWarnings("unchecked")
	@Override
	public Userinfo findUserinfoByProperties(final Userinfo userinfo)
			throws Exception {
		hql = "from Userinfo as u where u.username=? and u.userpwd=?";
		//sessionFactory--session --Query.setString()
		return this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setString(0, userinfo.getUsername());
				query.setString(1, userinfo.getUserpwd());
				
				return query.uniqueResult();//query.list()
			}
			
		});
		
	
	}

	@Override
	public Userinfo get(Short id) throws Exception {
		
		return this.getHibernateTemplate().get(Userinfo.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Userinfo> getAllUser() throws Exception {
		hql = "from Userinfo";
		//sessionFactory--session --Query.setString()
		return this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				return query.list();//query.list()
			}
			
		});
	}

}
